<template>
  <div class="shopcar_contaniner">
    <div class="goodslist" v-for="(item,i) in newShopCarList" :key="item.id">
      <!-- 商品列表详情 -->
      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch
              @change="selectedChanged(item.id , $store.getters.getGoodsSelected[item.id])"
              v-model="$store.getters.getGoodsSelected[item.id]"
            ></mt-switch>
            <img :src="item.img_Url">
            <div class="info">
              <h1>{{ item.title }}</h1>
              <p>
                <span class="price">￥{{ item.sell_price }}</span>
                <numbox :shopCarcount="$store.getters.getShopCarcount[item.id]" :goodsid="item.id"></numbox>
                <a href="#" @click.prevent="remove(item.id,i)">删除</a>
                <!--  i 是删除newShopCarList里面的数据  item.id 是删除store里面 state中的数据-->
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 结算区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <p>总计不含运费：</p>
            <p>
              已勾选商品
              <span class="red">{{ $store.getters.getGoodsCountAndAmount.count }}</span>件，总价
              <span class="red">￥{{$store.getters.getGoodsCountAndAmount.amount}}</span>
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
    <!-- <p> 测试 {{ $store.getters.getGoodsSelected }}</p> -->
  </div>
</template>

<script>
import numbox from "../subcomponents/shopcar_number.vue";
export default {
  data() {
    return {
      goodsInfoData: [
        {
          id: 0,
          title: "HUAWEI P20 Pro 6GB+128GB",
          add_time: "2018-04-19T19:34:55.000Z",
          goos_no: "SD3973042344",
          stock_quantity: 420,
          market_price: 4699,
          sell_price: 4599,
          img_Url: require("../../images/gs3.png")
        },
        {
          id: 1,
          title: "HUAWEI Nova 6GB+128GB",
          add_time: "2018-04-19T19:34:55.000Z",
          goos_no: "SD3973042344",
          stock_quantity: 320,
          market_price: 2799,
          sell_price: 2599,
          img_Url: require("../../images/gs4.png")
        },
        {
          id: 2,
          title: "HUAWEI Mate Pro 6GB+128GB",
          add_time: "2018-04-19T19:34:55.000Z",
          goos_no: "SD3973042344",
          stock_quantity: 520,
          market_price: 3799,
          sell_price: 3599,
          img_Url: require("../../images/gs5.png")
        },
        {
          id: 3,
          title: "HUAWEI Nova3 Pro 6GB+128GB",
          add_time: "2018-04-19T19:34:55.000Z",
          goos_no: "SD3973042344",
          stock_quantity: 220,
          market_price: 2869,
          sell_price: 2699,
          img_Url: require("../../images/gs1.png")
        },
        {
          id: 4,
          title: "HUAWEI Nova2 Pro 6GB+128GB",
          add_time: "2018-04-19T19:34:55.000Z",
          goos_no: "SD3973042344",
          stock_quantity: 120,
          market_price: 2369,
          sell_price: 2199,
          img_Url: require("../../images/gs2.png")
        }
      ],
      newShopCarList: []
    };
  },
  components: {
    numbox
  },
  created() {
    this.shopcarlist();
  },
  methods: {
    shopcarlist() {
      var dataArr = [];
      this.$store.state.car.forEach(item => {
        dataArr.push(item.id);
      });
      if (dataArr <= 0) {
        return;
      }
      this.goodsInfoData.forEach(data => {
        dataArr.forEach(id => {
          if (id == data.id) {
            this.newShopCarList.push(data);
          }
        });
      });
      //   console.log(this.newShopCarList);
    },
    remove(id, index) {
      // 删除购物车的某一项
      this.newShopCarList.splice(index, 1); //删除 newShopCarList中当前的那一项

      this.$store.commit("removeCar", id); //删除 本地存储中的数据
    },
    selectedChanged(id, val) {
      // 每当点击开关  把开关的状态添加到 store 中
      this.$store.commit("updateGoodsSelected", { id, selected: val });
    }
  }
};
</script>

<style lang="scss" scoped>
.shopcar_contaniner {
  background-color: #eee;
  overflow: hidden;
  .goodslist {
    .mui-card-content-inner {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    img {
      width: 46px;
      height: 46px;
      margin: 0 2px;
    }

    h1 {
      font-size: 13px;
    }
    .info {
      .price {
        color: red;
        font-weight: bold;
        font-size: 13px;
      }
      p {
        margin-bottom: 0;
        div {
          margin: 0 0px;
        }
      }
    }
  }
  .jiesuan {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .red {
      color: red;
      font-weight: bold;
      font-size: 16px;
    }
  }
}
</style>
